package com.sun.controller;


import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sun.common.QueryPageParam;
import com.sun.common.Result;
import com.sun.entity.Storage;
import com.sun.entity.User;
import com.sun.mapper.StorageMapper;
import com.sun.mapper.UserMapper;
import com.sun.service.StorageService;
import com.sun.until.MyStringUntil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;

/**
 * <p>
 * 前端控制器：仓库管理模块
 * </p>
 *
 * @author rabbiter
 * @since 2023-01-05
 */
@RestController
@RequestMapping("/storage")
public class StorageController {
    @Autowired
    private StorageService storageService;

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private StorageMapper storageMapper;

    @Autowired
    private RedisTemplate<String, Object> redisTemplate ;

    /*
     * 新增仓库
     * @author rabbiter
     * @date 2023/1/5 19:36
     */
    @PostMapping("/save")
    public Result save(@RequestBody Storage storage) {
        String address = (String) redisTemplate.opsForValue().get("address");
        storage.setAddress(address);
        redisTemplate.delete("address");

        String addrPoint = (String) redisTemplate.opsForValue().get("addrpoint");
        storage.setAddrpoint(addrPoint);
        redisTemplate.delete("addrpoint");

        return storageService.save(storage) ? Result.success() : Result.fail();
    }

    /*
     * 更新仓库
     * @author rabbiter
     * @date 2023/1/5 19:38
     */
    @PostMapping("/update")
    public Result update(@RequestBody Storage storage) {
        return storageService.updateById(storage) ? Result.success() : Result.fail();
    }

    /*
     * 删除仓库
     * @author rabbiter
     * @date 2023/1/5 19:40
     */
    @GetMapping("/del")
    public Result del(@RequestParam String id) {
        return storageService.removeById(id) ? Result.success() : Result.fail();
    }

    /*
     * 查询仓库列表
     * @author rabbiter
     * @date 2023/1/5 19:42
     */
    @GetMapping("/list")
    public Result list() {
        List list = storageService.list();
        return Result.success(list);
    }

    @GetMapping("/typeList")
    public Result typeList() {
        List list = storageMapper.typeList();
        return Result.success(list);
    }

    @GetMapping("/cellList")
    public Result cellList() {
        List<String> list = storageService.cellList();
        return Result.success(list);
    }

    @GetMapping("/myCellList")
    public Result myCellList() {
        //权限系统
        String json = (String) redisTemplate.opsForValue().get("user");
        User user1 = JSONUtil.toBean(json, User.class);

        Integer id = user1.getId();
        String user1Name = user1.getName();

        List<String> list = storageService.myCellList(id,user1Name);
        return Result.success(list);
    }

    /*
     * 模糊查询：根据输入查询仓库并以分页的形式展示
     * @author rabbiter
     * @date 2023/1/5 19:43
     */
    @PostMapping("/listPage")
    public Result listPage(@RequestBody QueryPageParam query) {
        HashMap param = query.getParam();
        String cell = (String) param.get("cell");
        String name = (String) param.get("name");
        String address = (String) param.get("address");
        Page<Storage> page = new Page();
        page.setCurrent(query.getPageNum());
        page.setSize(query.getPageSize());
        LambdaQueryWrapper<Storage> queryWrapper = new LambdaQueryWrapper<>();
        if (StringUtils.isNotBlank(cell) && !"null".equals(cell)) {
            queryWrapper.like(Storage::getCell, cell);
        }
        if (StringUtils.isNotBlank(name) && !"null".equals(name)) {
            queryWrapper.like(Storage::getName, name);
        }
        if (StringUtils.isNotBlank(address) && !"null".equals(address)) {
            queryWrapper.like(Storage::getAddress, address);
        }
        IPage result = storageService.pageCC(page, queryWrapper);
        List<Storage> records = result.getRecords();
        for (Storage record : records) {
            String admin = record.getAdmin();
            QueryWrapper<User> userQueryWrapper = new QueryWrapper<>();
            if (MyStringUntil.isNumeric(admin)) {
                userQueryWrapper.eq("id", Integer.valueOf(admin)); //将xx替换为你要查询的id
                User user = userMapper.selectOne(userQueryWrapper);
                record.setAdmin(user.getName());
            }
        }
        return Result.success(result.getRecords(), result.getTotal());
    }

    @PostMapping("/address")
    public Result address(@RequestBody HashMap<String, Object> param) {
//        {
//  "lng": 117.13368176985533,
//  "lat": 36.66753556284382
//}

        String address = (String) param.get("address");
        redisTemplate.opsForValue().set("address", address);
        HashMap<String, String> addrPoint = (HashMap<String, String>) param.get("addrPoint");
        String jsonStr = JSONUtil.toJsonStr(addrPoint);
        redisTemplate.opsForValue().set("addrpoint", jsonStr);
        return Result.success();
    }

}
